/**
* file name  : it/ciano/jenoware/Sbj.java
* authors    : Luciano Xumerle
* created    : mer 25 ago 2010, 19.32.38, CEST
*
* Copyright (c) 2010 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package it.ciano.jenoware;

import java.util.*;

/**
 * The class manages Pedigree informations of the subject.
 *
 * @author Luciano Xumerle
 * @version 0.0.1
 */
public class SbjFml extends HashMap {

    /**
     * Class constructor
     */
    public SbjFml ( ) {
        super ();
    }


    /**
     * Adds a family to the list.
     *
     * @param familyID the family ID.
     * @param subjectID the subject ID.
     * @param fatherID the father ID.
     * @param motherID the mother ID.
     * @param proband proband informations (1/0).
     * @return True or False.
     */
    public boolean addFam ( String familyID, String subjectID, String fatherID, String motherID, String proband ) {
        Value fam=new Value ( familyID, true );
        if ( fam.isValid()  &&   !containsKey ( familyID ) ) {
            Value[] v = new Value[] { new Value ( subjectID, true ),
                    new Value ( fatherID, true ),
                    new Value ( motherID, true ),
                    new Value ( proband, true )
                                    };
            put ( fam.getValue(), v );
            return true;
        }
        return false;
    }


    /**
     * Adds a family to the list.
     *
     * @param familyID the family ID.
     * @return True or False.
     */
    public boolean addFam ( String familyID ) {
        return addFam ( familyID, "", "", "", "" );
    }


    final private String getFieldID ( String familyID, int index ) {
        if ( containsKey ( familyID ) ) {
            Value[] v = ( Value[] ) get ( familyID );
            return v[index].toString();
        }
        return "";
    }

    final private boolean setFieldID ( String familyID, String val, int index ) {
        if ( containsKey ( familyID ) ) {
            Value[] v = ( Value[] ) get ( familyID );
            v[index]=new Value ( val, true );
            put ( familyID, v );
            return true;
        }
        return false;
    }


    /**
     * Returns the subjectID value.
     *
     * @param familyID the family ID.
     * @return the subjectID value.
     */
    final public String getSubjectID ( String familyID ) {
        return getFieldID ( familyID, 0 );
    }


    /**
     * Sets the value of subject ID.
     *
     * @param familyID the family ID.
     * @param subjectID the subject ID.
     * @return True or False.
     */
    final public boolean setSubjectID ( String familyID, String subjectID ) {
        return setFieldID ( familyID, subjectID, 0 );
    }


    /**
     * Returns the fatherID value.
     *
     * @param familyID the family ID.
     * @return the father ID value.
     */
    final public String getFatherID ( String familyID ) {
        return getFieldID ( familyID, 1 );
    }


    /**
     * Sets the value of father ID.
     *
     * @param familyID the family ID.
     * @param fatherID the father ID.
     * @return True or False.
     */
    final public boolean setFatherID ( String familyID, String fatherID ) {
        return setFieldID ( familyID, fatherID, 1 );
    }


    /**
     * Returns the motherID value.
     *
     * @param familyID the family ID.
     * @return the motherID value.
     */
    final public String getMotherID ( String familyID ) {
        return getFieldID ( familyID, 2 );
    }


    /**
     * Sets the value of mother ID.
     *
     * @param familyID the family ID.
     * @param fatherID the mother ID.
     * @return True or False.
     */
    final public boolean setMotherID ( String familyID, String motherID ) {
        return setFieldID ( familyID, motherID, 2 );
    }


    /**
     * Returns the proband value.
     *
     * @param familyID the family ID.
     * @return the proband value.
     */
    final public String getProband ( String familyID ) {
        return getFieldID ( familyID, 3 );
    }


    /**
     * Sets the value of proband.
     *
     * @param familyID the family ID.
     * @param proband the proband.
     * @return True or False.
     */
    final public boolean setProband ( String familyID, String proband ) {
        return setFieldID ( familyID, proband, 3 );
    }


    /**
     * Returns the Family IDs.
     *
     * @return the Family IDs or null value.
     */
    final public String[] getFamilies() {
        if ( size() == 0 )
            return null;
        String res[] = new String[ size() ];
        int i = 0;
        for ( Iterator it = entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = ( Map.Entry ) it.next();
            res[ i++ ] = ( String ) entry.getKey();
        }
        Arrays.sort ( res );
        return res;
    }

} // end class

